package cn.signalpha.modules.shop.service;

import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.shop.entity.Pattern;
import cn.signalpha.modules.shop.mapper.PatternMapper;
import cn.signalpha.modules.shop.vo.PatternVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 服务类
 *
 * @author signalpha
 */
@Service
public class PatternService extends ServiceImpl<PatternMapper, Pattern> {

	/**
	 * 自定义分页
	 */
    public IPage<PatternVO> selectPatternPage(IPage<PatternVO> page, Map<String, Object> params) {
        return page.setRecords(baseMapper.selectPatternPage(page, params));
    }

    /**
	 * 自定义Wrappers
	 */
    public QueryWrapper<Pattern> selectPatternWrapper(Map<String, Object> params) {
        QueryWrapper<Pattern> wrapper = Wrappers.query();
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("id")), Pattern::getId, params.get("id"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("sellerId")), Pattern::getSellerId, params.get("sellerId"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("type")), Pattern::getType, params.get("type"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("title")), Pattern::getTitle, params.get("title"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("config")), Pattern::getConfig, params.get("config"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("createTime")), Pattern::getCreateTime, params.get("createTime"));
        wrapper.lambda().orderByDesc(Pattern::getId);
        return wrapper;
    }

	/**
	 * 根据商户查询
	 */
    public Pattern getBySellerIdAndType(Long sellerId, String type) {
		return baseMapper.selectOne(Wrappers.<Pattern>lambdaQuery().eq(Pattern::getSellerId, sellerId).eq(Pattern::getType, type));
    }
}

